package com.yuanqunsoft.alimigration.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.yuanqunsoft.alimigration.bean.RuleBean;
import com.yuanqunsoft.alimigration.dao.IRemediationDao;
import com.yuanqunsoft.alimigration.dao.IRuleDao;
import com.yuanqunsoft.alimigration.model.Rule;
import com.yuanqunsoft.alimigration.service.IRuleService;

public class RuleServiceImpl implements IRuleService {

    private IRuleDao ruleDao;
    private IRemediationDao remediationDao;

    @Override
    public boolean addRule(RuleBean ruleBean, int remediationId) {
        // TODO Auto-generated method stub
        Rule r = getRuleFromBean(ruleBean);
        r.setRemediation(this.remediationDao.getRemediationById(remediationId));
        return this.ruleDao.save(r);
    }

    @Override
    public boolean deleteRule(RuleBean ruleBean) {
        // TODO Auto-generated method stub
        Rule r = this.ruleDao.getRuleById(ruleBean.getRulId());
        return this.ruleDao.delete(r);
    }

    @Override
    public boolean update(RuleBean ruleBean) {
        // TODO Auto-generated method stub
        Rule r = this.ruleDao.getRuleById(ruleBean.getRulId());
        r.setRulAllFurtherDayEach(ruleBean.getRulAllFurtherDayEach());
        r.setRulCategory(ruleBean.getRulCategory());
        r.setRulDescription(ruleBean.getRulDescription());
        r.setRulEnabled(ruleBean.getRulEnabled());
        r.setRulFirstAlerts(ruleBean.getRulFirstAlerts());
        r.setRulFirstDaysEach(ruleBean.getRulFirstDaysEach());
        r.setRulName(ruleBean.getRulName());
        r.setRulNextAlerts(ruleBean.getRulNextAlerts());
        r.setRulNextDaysEach(ruleBean.getRulNextDaysEach());
        r.setRulSeverity(ruleBean.getRulSeverity());
        return this.ruleDao.update(r);
    }

    @Override
    public RuleBean getRuleById(int id) {
        // TODO Auto-generated method stub
        return transToBean(this.ruleDao.getRuleById(id));
    }
    
    @Override
    public Rule getRuleByName(String name){
        return this.ruleDao.getRuleByName(name);
    }

    @Override
    public List<RuleBean> getAllRule(int currentPage, int pageSize) {
        // TODO Auto-generated method stub
        return transToBean(ruleDao.queryPage(currentPage, pageSize));
    }

    @Override
    public List<RuleBean> getAllRule() {
        // TODO Auto-generated method stub
        return transToBean(ruleDao.getAllRules());
    }

    @Override
    public int getAllPage() {
        // TODO Auto-generated method stub
        return ruleDao.getAllPage();
    }

    @Override
    public void selectRemediation(RuleBean ruleBean, int remediationId) {
        // TODO Auto-generated method stub
        Rule r = this.ruleDao.getRuleById(ruleBean.getRulId());
        r.setRemediation(this.remediationDao.getRemediationById(remediationId));
        this.ruleDao.update(r);
    }

    public IRuleDao getRuleDao() {
        return ruleDao;
    }

    public void setRuleDao(IRuleDao ruleDao) {
        this.ruleDao = ruleDao;
    }

    public IRemediationDao getRemediationDao() {
        return remediationDao;
    }

    public void setRemediationDao(IRemediationDao remediationDao) {
        this.remediationDao = remediationDao;
    }

    @Override
    public boolean isRuleNameExisted(RuleBean ruleBean) {
        // TODO Auto-generated method stub
        List<Rule> r = this.ruleDao.getAllRules();
        Iterator<Rule> i = r.iterator();
        while(i.hasNext()){
            if(i.next().getRulName().equals(ruleBean.getRulName())){
                return true;
            }
        }
        return false;
    }
    
    public RuleBean transToBean(Rule rule){
        
        RuleBean rb = new RuleBean();
        rb.setRulAllFurtherDayEach(rule.getRulAllFurtherDayEach());
        rb.setRulCategory(rule.getRulCategory());
        rb.setRulDescription(rule.getRulDescription());
        rb.setRulEnabled(rule.getRulEnabled());
        rb.setRulFirstAlerts(rule.getRulFirstAlerts());
        rb.setRulFirstDaysEach(rule.getRulFirstDaysEach());
        rb.setRulNextAlerts(rule.getRulNextAlerts());
        rb.setRulNextDaysEach(rule.getRulNextDaysEach());
        rb.setRulId(rule.getRulId());
        rb.setRulName(rule.getRulName());
        rb.setRulSeverity(rule.getRulSeverity());
        return  rb;
    }
    
    public List<RuleBean> transToBean(List<Rule> rules){
        Iterator<Rule> i = rules.iterator();
        List<RuleBean> rbs = new ArrayList<RuleBean>();
        Rule rule;
        RuleBean rb;
        while(i.hasNext()){
            rule = i.next();
            rb = new RuleBean();
            rb.setRulAllFurtherDayEach(rule.getRulAllFurtherDayEach());
            rb.setRulCategory(rule.getRulCategory());
            rb.setRulDescription(rule.getRulDescription());
            rb.setRulEnabled(rule.getRulEnabled());
            rb.setRulFirstAlerts(rule.getRulFirstAlerts());
            rb.setRulFirstDaysEach(rule.getRulFirstDaysEach());
            rb.setRulNextAlerts(rule.getRulNextAlerts());
            rb.setRulNextDaysEach(rule.getRulNextDaysEach());
            rb.setRulId(rule.getRulId());
            rb.setRulName(rule.getRulName());
            rb.setRulSeverity(rule.getRulSeverity());
            rbs.add(rb);
        }
       return rbs;
    }
    
    public Rule getRuleFromBean(RuleBean rule) {
        
        Rule rb = new Rule();
        rb.setRulAllFurtherDayEach(rule.getRulAllFurtherDayEach());
        rb.setRulCategory(rule.getRulCategory());
        rb.setRulDescription(rule.getRulDescription());
        rb.setRulEnabled(rule.getRulEnabled());
        rb.setRulFirstAlerts(rule.getRulFirstAlerts());
        rb.setRulFirstDaysEach(rule.getRulFirstDaysEach());
        rb.setRulNextAlerts(rule.getRulNextAlerts());
        rb.setRulNextDaysEach(rule.getRulNextDaysEach());
        rb.setRulId(rule.getRulId());
        rb.setRulName(rule.getRulName());
        rb.setRulSeverity(rule.getRulSeverity());
        return rb;
       
    }

}
